package com.hyf.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.math.BigDecimal;

/**
 * Show Auto-Type effect
 *
 * @author baB_hyf
 * @date 2021/03/28
 */
public class T1 {

    public static void main(String[] args) {
        Store store = new Store();
        Apple apple = new Apple();
        apple.setPrice(BigDecimal.valueOf(0.5));
        store.setFruit(apple);

        // String jsonString = JSON.toJSONString(store); // Non auto type
        String jsonString = JSON.toJSONString(store, SerializerFeature.WriteClassName, SerializerFeature.PrettyFormat);
        System.out.println("toJSONString : \n" + jsonString);

        Store newStore = JSON.parseObject(jsonString, Store.class);
        System.out.println("parseObject : " + newStore);
        Fruit newFruit = newStore.getFruit();
        System.out.println("getFruit : " + newFruit);
        Apple newApple = (Apple) newStore.getFruit();
        System.out.println("getApple : " + newApple);
    }

    interface Fruit {
    }

    static class Store {

        private Fruit fruit;

        public Fruit getFruit() {
            return fruit;
        }

        public void setFruit(Fruit fruit) {
            this.fruit = fruit;
        }
    }

    static class Apple implements Fruit {

        private BigDecimal price;

        public BigDecimal getPrice() {
            return price;
        }

        public void setPrice(BigDecimal price) {
            this.price = price;
        }
    }
}
